package personal.zq.generator.sys.config.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    //api接口包扫描路径
    public static final String VERSION = "1.0.0";
    @Value("${swagger.title}")
    private String title;
    @Value("${swagger.description}")
    private String description;
    @Value("${swagger.contact.names}")
    private String contactNames;
    @Value("${swagger.scanPackage}")
    private String scanPackage;
    @Value("${swagger.contact.url}")
    private String contactUrl;
    @Value("${swagger.contact.email}")
    private String contactEmail;

    @Bean
    public Docket createRestApi() {
        // 可以根据url路径设置哪些请求加入文档，忽略哪些请求
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(scanPackage)).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title) //设置文档的标题
                .description(description) // 设置文档的描述
                .version(VERSION) // 设置文档的版本信息-
                .contact(new Contact(contactNames, contactUrl, contactEmail)).termsOfServiceUrl("http://localhost" +
                        ":8001")
                .build();
    }
}
